package org.judo.generate.mvc;

public class ActionParser implements Parser
{
	ActionCodeResult actionResult;
	ParseResults parseResults;
	String curStatement;
	Parser nextParser;
	ActionCodeContainer container;
	
	public ActionCodeContainer getContainer()
	{
		return container;
	}

	public void setContainer(ActionCodeContainer container)
	{
		this.container = container;
	}

	public ParseResults getParseResults()
	{
		return parseResults;
	}

	public void setParseResults(ParseResults parseResults)
	{
		this.parseResults = parseResults;
	}
	
	public Parser getNestedParser()
	{
		return nextParser;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/action"))
			{
				return true;
			}
				
		return false;
	}

	public boolean needsNestedParser()
	{
		if (nextParser != null)
			return true;
		
		return false;
	}

	public void parseNextLine(String line) throws ParseException
	{
		nextParser = null;
		curStatement = line;
		
		if (line.startsWith("/action"))
		{
		}
		else
		{
			actionResult.javaCode += "\t\t"+line+"\n";
		}
	}
	
	public void setInitLine(String line) throws ParseException
	{	
		actionResult = new ActionCodeResult();
		
		String split[] = line.split(":");
		
		if (split.length == 1)
		{
			throw new ParseException("Action name not specified in init line");
		}
		
		if (split[1].equals(""))
			throw new ParseException("Action name not specified in init line");
		
		String name = split[1].trim();
		
		if (!name.contains("/"))
			throw new ParseException("Action names must be in the format [controller_name].[url_action_name]");
		
		String parts[] = name.split("/");
		
		if (parts.length != 2)
			throw new ParseException("Action names must be in the format [controller_name]/[url_action_name]");
		
		actionResult.actionName = parts[1].trim();
		actionResult.contollerName = parts[0].trim();
		parseResults.actionResults.put(actionResult.contollerName+"."+actionResult.contollerName,actionResult);
	}		
}

